#include <fstream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <boost/program_options.hpp>
#include <string>
#include <math.h>
#include "get_arg.h"


// link with -lboost_filesystem-gcc -lboost_program_options-gcc
// program_options_helper.cpp

// my functions
#define DEBUG_LEVEL 6
#include <debug_func.h>
#include <matrix_formats.h>
#include <print_error.h>
#include <program_options_helper.h>

using std::string;
using std::cerr;
using namespace program_options_helper;
namespace po = boost::program_options_lg;



namespace{
void select_cutoff_format(const char arg, eCUTOFF_FORMAT& format)
{
	switch (arg)
	 {
		 case 't':
			 format= eCUTOFF_M1;
			 break;
		 case 'm':
			 format= eCUTOFF_M2;
			 break;
		 case 'b':
			 format= eCUTOFF_BOTH;
			 break;
		case 'n':
			format= eCUTOFF_NONE;
			break;
		 default:
			 throw (std::logic_error(
								"The cutoff format must be one of \n"
								 "\tt    : only apply cutoff to Tree branch distances\n"
								 "\tm    : only apply cutoff to Matrix distances\n"
								 "\tb    : apply cutoff to Both tree and matrix distances\n"
								 "\tn    : apply cutoff to Neither\n"));

	 }
}
}



bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{

	// mandatory arguments
	string matrix_file_name;
	po::options_description mandatory_args("Mandatory Arguments");
	mandatory_args.add_options()
	("matrix_file,m", po::value<string>(&matrix_file_name)->set_name("FILE"),
									"Matrix data in the specified format");


	// optional arguments
	char input_format;
	char cutoff_format;
	args.optional_args.add_options()
	("input_format,i", po::value<char>(&input_format)->default_value('p')->set_name("FORMAT"),
									"Input FORMAT is binary ('b'), pairs ('p'), "
									"square ('s') or lower triangular ('l') .")
	("error_if_matrix_incomplete,E","Treat entries which are present in"
									   "the tree but absent in the matrix as "
									   "errors. Defaults to false. If the "
									   "verbose setting is on, missing entries "
									   "are listed on STDERR. ")
	("cutoff,c", po::value<float>(&args.cutoff)->default_value(50.0)->set_name("NUMBER"),
									"Only compare matrix values less than NUMBER.")
	("partial_cutoff,p", po::value<char>(&cutoff_format)->default_value('n')->set_name("OPTION"),
									"The cutoff can be applied to only Tree ('t'), "
										"Matrix ('m') distances, Both ('b') or "
										"Neither ('n').")
	("header,H",					"Add a header line (matrix name prefixed with '>') "
									"to the matrix output.");

	args.add_std_options();
	po::options_description all_args("");
	all_args.add(args.optional_args);

	// parse command line
	po::variables_map vm;
	po::store(po::command_line_parser(argc, argv).options(all_args).run(), vm);
	po::notify(vm);

	if (vm.count("version"))
	{
		print_version(cerr, *argv, "1.0\n"
			"\tHistory: \n\n"
			"\tv 1.0\1"
			"Initial version\n"
//			"\tv 1.1\1"
//			"01-02-2005\1"
//			"Split programme so that vanilla version does not do anything with matrices.\n"
			);
		return false;
	}
	if (vm.count("help"))
	{
		string exe_description =
			"Takes a Ks distance matrix and a tree in New Hamshire format "
			"from STDIN and outputs the differences between the matrix and the "
			"corresponding distances on the trees.\n\n";
		print_usage(cerr, string(*argv), "[OPTIONS] --matrix_file MATRIX_FILE < TREE",
							all_args, exe_description, 80);
		return false;
	}

	args.header						= vm.count("header");
	args.missing_matrix_as_errors	= vm.count("error_if_matrix_incomplete");


	select_matrix_file_format(input_format, args.input_format, "input format");
	select_cutoff_format(cutoff_format, args.cutoff_format);

	args.matrix_strm.open(matrix_file_name.c_str());
	if (!args.matrix_strm)
		throw (std::runtime_error("Could not open " + matrix_file_name));

	args.open_err_log();
	return true;
}



